package com.ch04.ex47;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * Created by zc on 2016/2/19.
 */
public class Main {
    public static void main(String[] args){
        ForkJoinPool pool = new ForkJoinPool();

        Folderprocessor system = new Folderprocessor("C:\\Windows", "log");
        Folderprocessor apps = new Folderprocessor("C:\\Program Files (x86)", "log");
        Folderprocessor documents = new Folderprocessor("C:\\Users\\", "log");

        pool.execute(system);
        pool.execute(apps);
        pool.execute(documents);

        do {
            System.out.printf("****************************\n");
            System.out.printf("Main: Parallelism: %d\n",
                    pool.getParallelism());
            System.out.printf("Main: Active Threads: %d\n",
                    pool.getActiveThreadCount());
            System.out.printf("Main: Task Count: %d\n",
                    pool.getQueuedTaskCount());
            System.out.printf("Main: Steal Count: %d\n",
                    pool.getStealCount());
            System.out.printf("****************************\n");

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while ((!system.isDone())
                || (!apps.isDone())
                || (!documents.isDone()));

        pool.shutdown();

        List<String> results;

        results = system.join();
        System.out.printf("System: %d files found.\n", results.size());

        results = apps.join();
        System.out.printf("Apps: %d files found.\n", results.size());

        results = documents.join();
        System.out.printf("Documents: %d files found.\n", results.size());
    }
}
